package com.sq.subject.application.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.sq.subject.application.dto.SubjectCategoryDTO;
import com.sq.subject.application.dto.SubjectLabelDTO;
import com.sq.subject.common.entity.Result;
import com.sq.subject.domain.bo.SubjectCategoryBO;
import com.sq.subject.domain.bo.SubjectLabelBO;
import com.sq.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author quan
 */
@Slf4j
@RestController
@RequestMapping("/subject/label")
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> addSubjectLabel(@RequestBody SubjectLabelDTO labelDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.addSubjectLabel.dto：{}", JSON.toJSONString(labelDTO));
        }
        try {
            Preconditions.checkNotNull(labelDTO.getCategoryId(), "标签所属分类id不能为空");
            Preconditions.checkNotNull(labelDTO.getSortNum(), "标签的排序不能为空");
            Preconditions.checkArgument(!StrUtil.isEmpty(labelDTO.getLabelName()), "标签名称不能为空");
            SubjectLabelBO subjectLabelBO =  BeanUtil.copyProperties(labelDTO, SubjectLabelBO.class);
            subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLabelController.addSubjectLabel.error:{}", e.getMessage(), e);
            return Result.fail("新增题目标签失败");
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateSubjectLabel(@RequestBody SubjectLabelDTO labelDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.updateSubjectLabel.dto：{}", JSON.toJSONString(labelDTO));
        }
        try {
            Preconditions.checkNotNull(labelDTO.getId(), "标签id不能为空");
            SubjectLabelBO subjectLabelBO =  BeanUtil.copyProperties(labelDTO, SubjectLabelBO.class);
            subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLabelController.updateSubjectLabel.error:{}", e.getMessage(), e);
            return Result.fail("修改题目标签失败");
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> deleteSubjectLabel(@RequestBody SubjectLabelDTO labelDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.deleteSubjectLabel.dto：{}", JSON.toJSONString(labelDTO));
        }
        try {
            Preconditions.checkNotNull(labelDTO.getId(), "标签id不能为空");
            SubjectLabelBO subjectLabelBO =  BeanUtil.copyProperties(labelDTO, SubjectLabelBO.class);
            subjectLabelDomainService.delete(subjectLabelBO);
            return Result.ok();
        } catch (Exception e) {
            log.error("SubjectLabelController.deleteSubjectLabel.error:{}", e.getMessage(), e);
            return Result.fail("修改题目标签失败");
        }
    }

    /**
     * 根据题目分类查询题目标签
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO labelDTO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.queryLabelByCategoryId.dto：{}", JSON.toJSONString(labelDTO));
        }
        try {
            Preconditions.checkNotNull(labelDTO.getCategoryId(), "分类id不能为空");
            SubjectLabelBO subjectLabelBO =   BeanUtil.copyProperties(labelDTO, SubjectLabelBO.class);
            List<SubjectLabelBO> subjectLabelBOList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOList = BeanUtil.copyToList(subjectLabelBOList, SubjectLabelDTO.class);
            return Result.ok(subjectLabelDTOList);
        } catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId.error:{}", e.getMessage(), e);
            return Result.fail("根据题目分类查询标签失败");
        }
    }
}
